package me.luraframework.db.repository;

import me.luraframework.db.criteria.QueryHelper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * @ description: BaseJpaRepositoryImpl
 * @ author: Liu Ran
 * @ data: 3/3/23 15:09
 */
public class BaseJpaRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements BaseJpaRepository<T, ID> {

    public BaseJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
    }

    public BaseJpaRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
    }

    @Override
    public List<T> findAll(Object criteria) {
        return findAll((root, query, criteriaBuilder) ->
                QueryHelper.getPredicate(root, criteria, criteriaBuilder));
    }

    @Override
    public List<T> findAll(Object criteria, Sort sort) {
        return findAll((root, query, criteriaBuilder) ->
                QueryHelper.getPredicate(root, criteria, criteriaBuilder), sort);
    }

    @Override
    public Page<T> findAll(Object criteria, Pageable pageable) {
        return super.findAll((root, query, criteriaBuilder) ->
                QueryHelper.getPredicate(root, criteria, criteriaBuilder), pageable);
    }
}
